<template>
	<view>
		<view id="_drag_button" class="drag" :style="buttonStyle" @touchstart="touchstart"
			@touchmove.stop.prevent="touchmove" @touchend="touchend" @click.stop.prevent="click"
			:class="{transition: isDock && !isMove }">
			<button class="btn" style="border: none;padding: 0;margin: 0;">
				<image class="img" :src="require(`../../static/logo.png`)">
				</image>
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'drag-button',
		props: {
			isDock: {
				type: Boolean,
				default: false
			},
			existTabBar: {}
		},
		data() {
			return {
				top: 0,
				left: 0,
				width: 0,
				height: 0,
				offsetWidth: 0,
				offsetHeight: 0,
				windowWidth: 0,
				windowHeight: 0,
				isMove: true,
				edge: 10,
				text: ' ',
				firstIn: false,
			}
		},
		computed: {
			// Style不支持字符串模板，用计算属性
			buttonStyle() {
				const opacity = this.firstIn ? '1' : '0'
				return `top:${this.top}px;left:${this.left}px;opacity:${opacity}`;
			},
		},
		mounted() {
			//获取手机信息配置接口
			const sys = uni.getSystemInfoSync();
			//屏幕的宽高
			this.windowWidth = sys.windowWidth;
			this.windowHeight = sys.windowHeight;
			// #ifdef APP-PLUS
			this.existTabBar && (this.windowHeight -= 50);
			// #endif
			if (sys.windowTop) {
				this.windowHeight += sys.windowTop;
			}
			//获取元素
			const query = uni.createSelectorQuery().in(this);
			query.select('#_drag_button').boundingClientRect(data => {
				console.log(data);
				this.width = data.width;
				this.height = data.height;
				this.offsetWidth = data.width / 2;
				this.offsetHeight = data.height / 2;
				// this.left = this.windowWidth - this.width - this.edge;
				// this.top = this.windowHeight - this.height - this.edge;
				this.left = uni.getStorageSync('left') ? uni.getStorageSync('left') : 50
				this.top = uni.getStorageSync('top') ? uni.getStorageSync('top') : 50
				this.$nextTick(() => {
					this.firstIn = true
				})
			}).exec();
		},
		methods: {
			click() {
				this.$emit('btnClick');
			},
			touchstart(e) {
				this.$emit('btnTouchstart');
			},
			touchmove(e) {
				// 单指触摸
				if (e.touches.length !== 1) {
					return false;
				}
				console.log('移动', e);
				this.isMove = true;
				this.left = e.touches[0].clientX - this.offsetWidth;
				let clientY = e.touches[0].clientY - this.offsetHeight;
				// #ifdef H5
				clientY += this.height;
				// #endif
				let edgeBottom = this.windowHeight - this.height - this.edge;
				// 上下触及边界
				if (clientY < this.edge) {
					this.top = this.edge;
				} else if (clientY > edgeBottom) {
					this.top = edgeBottom;
				} else {
					this.top = clientY
					uni.setStorageSync("top", this.top);
				}
				console.log(this.top)
			},
			touchend(e) {
				if (this.isDock) {
					console.log(e)
					let edgeRigth = this.windowWidth - this.width - this.edge;
					if (this.left < this.windowWidth / 2 - this.offsetWidth) {
						this.left = this.edge;
					} else {
						this.left = edgeRigth;
					}
					uni.setStorageSync("left", this.left);
					this.isMove = false;
					this.$emit('btnTouchend');
				}
			}
		}
	}
</script>
<style lang="scss">
	.drag {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 180upx;
		height: 135upx;
		border-radius: 50%;
		font-size: $uni-font-size-sm;
		position: fixed;
		z-index: 999999;

		&.transition {
			transition: left .3s ease, top .3s ease;
		}
	}

	.btn {
		background-color: transparent;
		width: 135upx;
		height: 130upx;
		z-index: 9999;
	}

	button::after {
		border: none;
	}

	.img {
		height: 100%;
		width: 100%;
	}
</style>
